package com.cn.wanxi.service;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;

import java.util.ArrayList;
import java.util.List;

public class NewsService {
    public List<NewsModel> getNewsModelList() {
        NewsDao newsDao = new NewsDao();
        return setList(newsDao.hrefs, newsDao.contents, newsDao.titles);
    }

    public List<NewsModel> getNewsModeListForNewsJSP() {
        NewsDao newsDao = new NewsDao();
        return setList(newsDao.hrefsForNewsJSP, newsDao.contentsForNewsJSP, newsDao.titlesForNewsJSP);
    }

    private List<NewsModel> setList(String[] hrefs, String[] contents, String[] titles) {
        List<NewsModel> newsModelList = new ArrayList<>();
        for (int i = 0; i < hrefs.length; i++) {
            NewsModel newsModel = new NewsModel();
            newsModel.setHref(hrefs[i]);
            newsModel.setContent(contents[i]);
            newsModel.setTitle(titles[i]);
            newsModelList.add(newsModel);
        }
        return newsModelList;
    }
}
